package com.littcore.saap.crm.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.littcore.common.BeanManager;
import com.littcore.exception.BusiCodeException;
import com.littcore.exception.BusiException;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.crm.biz.ICustomerBizService;
import com.littcore.saap.crm.po.Customer;
import com.littcore.saap.crm.service.ICustContactsService;
import com.littcore.saap.crm.service.ICustomerService;
import com.littcore.saap.system.webservice.IUserWebService;
import com.littcore.shield.vo.ILoginVo;


/**
 * .
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年8月3日
 * @version 1.0
 */
public class CustomerBizServiceImpl implements ICustomerBizService {
  
  public static final Logger logger = LoggerFactory.getLogger(CustomerBizServiceImpl.class);
  @Resource
  private ICustomerService customerService;
  @Resource
  private ICustContactsService custContactsService;
  @Resource
  private IUserWebService userWebService;
  
  /* (non-Javadoc)
   * @see com.littcore.saap.crm.biz.impl.ICustomerBizService#imp(java.lang.String[], java.util.List, int, int, com.littcore.shield.vo.ILoginVo)
   */
  @Override
  public List<String> imp(String[] fieldNames, List<Map<String, Object>> dataList, int tenantId, int userId, ILoginVo loginUser)
  {
    List<String> failedRowList = new ArrayList<String>();
    
    for (int i = 0; i < dataList.size(); i++) {
      Map<String, Object> rowMap = dataList.get(i);
      try{
        this.saveRow(i, fieldNames, rowMap, tenantId, userId, loginUser);
      }
      catch (BusiCodeException e)
      {
        String errorMsg = BeanManager.getMessage(e.getErrorCode(), e.getParams(), loginUser.toLocale());
        logger.error("导入"+(i+1)+"行失败，原因："+errorMsg, e);
        failedRowList.add("第"+(i+1)+"行导入失败，原因："+errorMsg);
      }
      catch(Exception e)
      {
        logger.error("导入"+(i+1)+"行失败，原因："+e.getMessage(), e);
        failedRowList.add("第"+(i+1)+"行导入失败，原因："+e.getMessage());
      }
    }
    return failedRowList;
    
  }
  
  public void saveRow(int rowNo, String[] fieldNames, Map<String, Object> rowMap, int tenantId, int userId, ILoginVo loginUser)
  {
    //有效性判断
    if (!rowMap.containsKey("CODE") || rowMap.get("CODE")==null)
    {
      throw new BusiException("客户编号不能为空");
    }
    if (!rowMap.containsKey("NAME") || rowMap.get("NAME")==null)
    {
      throw new BusiException("客户名称不能为空");
    }
    if (!rowMap.containsKey("CHARGE_BY") || rowMap.get("CHARGE_BY")==null)
    {
      throw new BusiException("负责人不能为空");
    }
    if (!rowMap.containsKey("ADDRESS") || rowMap.get("ADDRESS")==null)
    {
      throw new BusiException("地址不能为空");
    }
    //
    Customer customer = new Customer();
    customer.setCode(rowMap.get("CODE").toString());
    customer.setName(rowMap.get("NAME").toString());
    if (rowMap.containsKey("SHORT_NAME") && rowMap.get("SHORT_NAME")!=null)
      customer.setShortName(rowMap.get("SHORT_NAME").toString());
    
    String chargeByName = rowMap.get("CHARGE_BY").toString();
    IUserInfo userInfo = userWebService.findByName(tenantId, chargeByName);
    if(userInfo==null)
      throw new BusiException("负责人:"+chargeByName+"不存在");
    customer.setChargeBy(userInfo.getId());
    
    //填充其他属性
    if (rowMap.containsKey("ACCOUNT_NO") && rowMap.get("ACCOUNT_NO")!=null)
      customer.setAccountNo(rowMap.get("ACCOUNT_NO").toString());
    if (rowMap.containsKey("ADDRESS") && rowMap.get("ADDRESS")!=null)
      customer.setAddress(rowMap.get("ADDRESS").toString());
    if (rowMap.containsKey("BANK_NAME") && rowMap.get("BANK_NAME")!=null)
      customer.setBankName(rowMap.get("BANK_NAME").toString());
    if (rowMap.containsKey("BILLING_ADDRESS") && rowMap.get("BILLING_ADDRESS")!=null)
      customer.setBillingAddress(rowMap.get("BILLING_ADDRESS").toString());
    if (rowMap.containsKey("BILLING_FULL_NAME") && rowMap.get("BILLING_FULL_NAME")!=null)
      customer.setBillingFullName(rowMap.get("BILLING_FULL_NAME").toString());
    if (rowMap.containsKey("BILLING_REMARK") && rowMap.get("BILLING_REMARK")!=null)
      customer.setBillingRemark(rowMap.get("BILLING_REMARK").toString());
    if (rowMap.containsKey("CONTACTS_ID") && rowMap.get("CONTACTS_ID")!=null)
    {
//      String contactsName = rowMap.get("CONTACTS_ID").toString();
//      customer.setContactsId(contactsId);
    }
    if (rowMap.containsKey("EMAIL") && rowMap.get("EMAIL")!=null)
      customer.setEmail(rowMap.get("EMAIL").toString());
    if (rowMap.containsKey("FAX") && rowMap.get("FAX")!=null)
      customer.setFax(rowMap.get("FAX").toString());
    if (rowMap.containsKey("MAILING_ADDRESS") && rowMap.get("MAILING_ADDRESS")!=null)
      customer.setMailingAddress(rowMap.get("MAILING_ADDRESS").toString());
    if (rowMap.containsKey("PHONE") && rowMap.get("PHONE")!=null)
      customer.setPhone(rowMap.get("PHONE").toString());
    if (rowMap.containsKey("REMARK") && rowMap.get("REMARK")!=null)
      customer.setRemark(rowMap.get("REMARK").toString());
    if (rowMap.containsKey("SHIPPING_ADDRESS") && rowMap.get("SHIPPING_ADDRESS")!=null)
      customer.setShippingAddress(rowMap.get("SHIPPING_ADDRESS").toString());
    else {
      customer.setShippingAddress(rowMap.get("ADDRESS").toString());
    }
    if (rowMap.containsKey("STATUS") && rowMap.get("STATUS")!=null)
    {
      customer.setStatus(5);//统一为已成交
      //customer.setStatus(rowMap.get("STATUS").toString());
    }
    if (rowMap.containsKey("TAX_NO") && rowMap.get("TAX_NO")!=null)
      customer.setTaxNo(rowMap.get("TAX_NO").toString());
    if (rowMap.containsKey("WEBSITE") && rowMap.get("WEBSITE")!=null)
      customer.setWebsite(rowMap.get("WEBSITE").toString());
    if (rowMap.containsKey("ZIP_CODE") && rowMap.get("ZIP_CODE")!=null)
      customer.setZipCode(rowMap.get("ZIP_CODE").toString());
    
    //填充默认值
    customer.setParentId(0);
    customer.setIsLeaf(true);
    customer.setIsDeleted(false);
    
    customerService.save(customer);
    
  }
  

}
